<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button
        :disabled="items.length > 0"
        class="filter-item"
        type="primary"
        icon="el-icon-plus"
        @click="showEditRedundancyDialog()"
      >添加
      </el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="tableLoading"
      :data="items"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <el-table-column :label="'ID'" prop="id" width="90" align="center"></el-table-column>
      <el-table-column :label="'名称'" prop="name"></el-table-column>
      <el-table-column :label="'本机主备类型'">
        <template slot-scope="scope">
          <span v-if="scope.row.type === 1">主网关</span>
          <span v-if="scope.row.type === 2">备网关</span>
        </template>
      </el-table-column>
      <el-table-column :label="'对端IP地址'" prop="ip"></el-table-column>
      <el-table-column :label="'心跳状态'" prop="ip" align="center">
        <template slot-scope="scope">
          <span style="color: #F56C6C" v-if="scope.row.status === -1">失败 <i class="el-icon-error"></i></span>
          <span style="color: #909399" v-if="scope.row.status === 0">未知 <i class="el-icon-remove"></i></span>
          <span style="color: #67C23A" v-if="scope.row.status === 1">成功 <i class="el-icon-success"></i></span>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="80" align="center">
        <template slot-scope="{ row }">
          <el-button
            type="text"
            @click="deleteData(row.id)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <editr-edundancy-dialog ref="editRedundancyDialog" @save="load()"></editr-edundancy-dialog>
  </div>
</template>

<script>
  import { getRedundancy, deleteRedundancy } from "@/api/redundancy";
  import EditRedundancyDialog from "./components/EditRedundancyDialog";
  import _ from "lodash";

  export default {
    name: "UserList",
    components: {
      "editr-edundancy-dialog": EditRedundancyDialog
    },
    data() {
      return {
        items: [],
        tableLoading: true,
        tableKey: 0,
        timer: undefined
      };
    },
    destroyed() {
      clearInterval(this.timer);
    },
    created() {
      this.load();
      this.setInterval();
    },
    methods: {
      load() {
        this.getList();
      },
      setInterval() {
        this.timer = setInterval(() => {
          this.getList();
        }, 3000);
      },
      getList() {
        this.tableLoading = true;
        getRedundancy().then(result => {
          this.items = result;
          this.tableLoading = false;
        });
      },
      sortChange(sortData) {
        const { prop, order } = sortData;
        if (prop && order === "descending") {
          this.q.sorting = prop + " desc";
        } else if (prop && order === "ascending") {
          this.q.sorting = prop;
        } else {
          this.q.sorting = "id";
        }
        this.load();
      },
      deleteData(id) {
        this.$confirm("删除后不可恢复， 是否继续?", "确定删除吗？", {
          type: "warning"
        })
          .then(() => {
            deleteRedundancy(id).then(() => {
              this.load();
              this.$message({ message: "删除成功", type: "success" });
            });
          })
          .catch(() => {
          });
      },

      showEditRedundancyDialog(id) {
        this.$refs["editRedundancyDialog"].show();
      }
    }
  };
</script>

<style scoped>
  .item {
    margin-top: 10px;
  }
</style>

